// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//
//  IOSByteArray.m
//  JreEmulation
//
//  Created by Tom Ball on 6/16/11.
//

#import "IOSByteArray.h"
#import "IOSPrimitiveClass.h"
//#import "java/lang/Byte.h"

@implementation IOSByteArray

+ (id)arrayWithBytes:(const char *)bytes count:(NSUInteger)count {
  return [[[self alloc] initWithBytes:bytes
                                        count:count] j2objc_autorelease];
}

- (id)initWithBytes:(const char *)bytes count:(NSUInteger)count {
  if ((self = [self initWithLength:count])) {
    if (NULL != bytes) {
      memcpy(self.storage.mutableBytes, bytes, self.storage.length);
    }
  }
  return self;
}

- (char)byteAtIndex:(NSUInteger)index {
  [self checkIndex:index];

  const char *buffer = (const char *)self.storage.bytes;
  return buffer[index];
}

- (char)replaceByteAtIndex:(NSUInteger)index withByte:(char)byte {
  [self checkIndex:index];
  
  char *buffer = (char *)self.storage.mutableBytes;
  buffer[index] = byte;
  return byte;
}

- (void)getBytes:(char *)buffer
          offset:(NSUInteger)offset
          length:(NSUInteger)length {
  [self checkRange:NSMakeRange(offset, length)];
  memcpy(buffer, self.storage.bytes, length /** [[self class] elementLength]*/);
}

- (void)replaceBytes:(char *)source
              length:(NSUInteger)length
              offset:(NSUInteger)destOffset {
  [self checkRange:NSMakeRange(destOffset, length)];
  memcpy(self.storage.mutableBytes, source,
         length /** [[self class] elementLength]*/);
}

- (char)incr:(NSUInteger)index {
  [self checkIndex:index];
  
  char *buffer = (char *)self.storage.mutableBytes;
  return ++buffer[index];
}

- (char)decr:(NSUInteger)index {
  [self checkIndex:index];
  
  char *buffer = (char *)self.storage.mutableBytes;
  return --buffer[index];
}

- (char)postIncr:(NSUInteger)index {
  [self checkIndex:index];
  
  char *buffer = (char *)self.storage.mutableBytes;
  return buffer[index]++;
}

- (char)postDecr:(NSUInteger)index {
  [self checkIndex:index];
  
  char *buffer = (char *)self.storage.mutableBytes;
  return buffer[index]--;
}

- (NSString *)descriptionOfElementAtIndex:(NSUInteger)index {
  const char *buffer = (const char *)self.storage.mutableBytes;
  return [NSString stringWithFormat:@"0x%x", buffer[index]];
}

- (IOSClass *)elementType {
  return [[[IOSPrimitiveClass alloc] initWithName:@"byte"
                                             type:@"B"] j2objc_autorelease];
}

+ (NSUInteger)elementLength
{
  return sizeof(char);
}

- (NSData *)toNSData {
  return [self.storage copy];
}

@end
